package com.cdp.zwy.zwy_manager_back_v1.controller;

import cn.hutool.core.date.DateUtil;
import com.cdp.zwy.zwy_manager_back_v1.common.Result;
import com.cdp.zwy.zwy_manager_back_v1.entity.Article;
import com.cdp.zwy.zwy_manager_back_v1.service.ArticleService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zwy
 * @version 1.0
 * @description: ArticleController文章管理后端控制器
 * @date 2025/4/14 19:02
 */
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PostMapping("/add")
    public Result add(@RequestBody Article article) {
        article.setTime(DateUtil.now()); // 获取当前最新的时间字符串
        if(article.getContent()=="" || article.getContent()==null){
            article.setContent("暂无内容");
        }else {
        article.setDescription(article.getContent().substring(0, 100));}
        System.out.println(article);
        return Result.success(articleService.add(article));
    }

    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(articleService.findAll());
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(articleService.findById(id));
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        return Result.success(articleService.deleteById(id));
    }

    @PutMapping("/modify")
    public Result modify(@RequestBody Article article) {
        return Result.success(articleService.modify(article));
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize) {
        return Result.success(articleService.selectPage(pageNum, pageSize));
    }

    @PostMapping("/findByTitle")
    public Result findByName(@RequestBody Article article, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize) {
        return Result.success(articleService.findByTitle(pageNum, pageSize, article.getTitle()));
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(articleService.deleteBatch(ids));
    }
    // ========= 修改的点赞相关接口 =========

    @PostMapping("/like/{articleId}/{userIdentifier}")
    public Result likeArticle(@PathVariable Integer articleId, @PathVariable String userIdentifier) {
        articleService.likeArticle(articleId, userIdentifier);
        return Result.success();
    }

    @DeleteMapping("/unlike/{articleId}/{userIdentifier}")
    public Result unlikeArticle(@PathVariable Integer articleId, @PathVariable String userIdentifier) {
        articleService.unlikeArticle(articleId, userIdentifier);
        return Result.success();
    }

    @GetMapping("/isLiked/{articleId}/{userIdentifier}")
    public Result isUserLikedArticle(@PathVariable Integer articleId, @PathVariable String userIdentifier) {
        return Result.success(articleService.isUserLikedArticle(articleId, userIdentifier));
    }

    // getArticleLikeCount 接口不需要改动
    @GetMapping("/like/count/{articleId}")
    public Result getArticleLikeCount(@PathVariable Integer articleId) {
        return Result.success(articleService.getArticleLikeCount(articleId));
    }
}